package com.lianzhihui.romeex.ui.me;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lianzhihui.romeex.MyApplication;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.config.MessageEvent;
import com.lianzhihui.romeex.ui.MainActivity;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.ui.me.dialog.ChoseCurrencyDialog;
import com.lianzhihui.romeex.ui.me.dialog.LanguageChoseDialog;
import com.lianzhihui.romeex.utils.LanguageType;
import com.lianzhihui.romeex.utils.LanguageUtil;
import com.lianzhihui.romeex.utils.NoDoubleClickUtils;
import com.lianzhihui.romeex.utils.PreferenceUtils;
import com.lianzhihui.romeex.utils.StatusBarUtil;
import com.lianzhihui.romeex.widget.OnOffView;

import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

import com.taobao.sophix.SophixManager;
import com.tencent.bugly.beta.Beta;

/**
 * @ProjectName: romeex
 * @Package: com.lianzhihui.romeex.ui.me
 * @ClassName: AboutUsActivity
 * @Description: java类作用描述
 * @Author: zhanghong
 * @CreateDate: 2020/11/2 16:36
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/11/2 16:36
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class SettingActivity extends BaseActivity {


    @BindView(R.id.status_bar_id)
    View statusBarId;
    @BindView(R.id.include_top_iv_back)
    ImageView includeTopIvBack;
    @BindView(R.id.include_top_lin_newback)
    LinearLayout includeTopLinNewback;
    @BindView(R.id.include_top_tv_title)
    TextView includeTopTvTitle;
    @BindView(R.id.include_top_iv_right_second)
    ImageView includeTopIvRightSecond;
    @BindView(R.id.include_top_tv_right)
    TextView includeTopTvRight;
    @BindView(R.id.include_top_lin_right)
    LinearLayout includeTopLinRight;
    @BindView(R.id.include_top_lin_background)
    LinearLayout includeTopLinBackground;
    @BindView(R.id.app_top_bar)
    LinearLayout appTopBar;
    @BindView(R.id.main_wallet_oc_lin_main)
    RelativeLayout mainWalletOcLinMain;
    @BindView(R.id.onOffView)
    OnOffView onOffView;
    @BindView(R.id.language_layout)
    RelativeLayout languageLayout;
    @BindView(R.id.money_unit_layout)
    RelativeLayout moneyUnitLayout;
    @BindView(R.id.update_layout)
    RelativeLayout updateLayout;
    @BindView(R.id.app_language)
    TextView appLanguage;
    @BindView(R.id.tv_currency)
    TextView tvCurrency;
    @BindView(R.id.update_version)
    TextView update_version;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        StatusBarUtil.setTransparent(this);
        StatusBarUtil.setStatusBarTextColor(this, false);
        includeTopTvTitle.setText(getResources().getText(R.string.text_setting));
        String strLanguage = PreferenceUtils.getString(AppConstants.LANGUAGE_TYPE, "").isEmpty() ? getResources().getString(R.string.text_simple_chinese) : PreferenceUtils.getString(AppConstants.LANGUAGE_TYPE, "");
        if (strLanguage.equals("zh")) {
            strLanguage = getResources().getString(R.string.text_simple_chinese);
        } else if (strLanguage.equals("en")) {
            strLanguage = getResources().getString(R.string.text_english);
        } else {
            strLanguage = getResources().getString(R.string.text_simple_chinese);
        }
        appLanguage.setText(strLanguage);
    }

    @Override
    protected void initData() {
        try {
            String versionName = MyApplication.mContext.getPackageManager().getPackageInfo(MyApplication.mContext.getPackageName(), 0).versionName;
            if (!versionName.isEmpty()) {
                update_version.setText("V" + versionName);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }


    @OnClick({R.id.include_top_iv_back,
            R.id.language_layout, R.id.money_unit_layout, R.id.update_layout})
    public void OnClick(View v) {
        if(NoDoubleClickUtils.isDoubleClick()){
            switch (v.getId()) {

                case R.id.include_top_iv_back:
                    finish();
                    break;
                case R.id.language_layout:
                    LanguageChoseDialog languageChoseDialog = new LanguageChoseDialog(this);
                    languageChoseDialog.setCancelable(false);
                    languageChoseDialog.bottom();
                    languageChoseDialog.fullWidth();
                    languageChoseDialog.show();
                    break;
                case R.id.money_unit_layout:
                    ChoseCurrencyDialog dialog = new ChoseCurrencyDialog(this);
                    dialog.setCancelable(false);
                    dialog.setDialogListener(s -> {
                        PreferenceUtils.applyString(AppConstants.CURRENCY_TYPE_KEY, s);
                        if (s.equals("cny")) {
                            tvCurrency.setText(getResources().getString(R.string.string_rmb));
                        } else {
                            tvCurrency.setText(getResources().getString(R.string.string_us));
                        }
                        dialog.dismiss();
                    });
                    dialog.bottom();
                    dialog.fullWidth();
                    dialog.show();
                    break;
                case R.id.update_layout:
                    checkUpdate();
//                VersionUpdateUtil.checkVersionUpdate(this, false);
                    break;

                default:
                    break;
            }
        }

    }

    private void checkUpdate() {
        /***** 检查热更新插件 *****/
//        SophixManager.getInstance().queryAndLoadNewPatch();
        /***** 检查更新 *****/
        Beta.checkUpgrade();
    }


    @Override
    protected void onResume() {
        super.onResume();
        String stringPreference = PreferenceUtils.getString(AppConstants.CURRENCY_TYPE_KEY, "");
        if (stringPreference.equals(AppConstants.CNY)) {
            tvCurrency.setText(getResources().getString(R.string.string_rmb));
        } else if (stringPreference.equals(AppConstants.USD)) {
            tvCurrency.setText(getResources().getString(R.string.string_us));
        }
//        String languagePreference = PreferenceUtils.getString(Constants.LANGUAGE_KEY, "");
//        if (languagePreference.equals()) {
//            tvCurrency.setText(getResources().getString(R.string.string_rmb));
//        } else {
//            tvCurrency.setText(getResources().getString(R.string.string_us));
//        }
    }

    @Override
    public void onMessageEvent(MessageEvent event) {
        super.onMessageEvent(event);
        if (event.getCode() == AppConstants.Event.LANGUAGECHANGE) {
            showLoading("");

            String valueLanguage = PreferenceUtils.getString(AppConstants.LANGUAGE_KEY, "");
            String stringType = PreferenceUtils.getString(AppConstants.LANGUAGE_TYPE, "");
            String language = setLanguageView(String.valueOf(event.getValue()));
            if (!valueLanguage.isEmpty()) {
                //不为空
                if (stringType.equals(event.getValue())) {
                    dismissLoading();
                    return;
                }

                if (null != language && !language.isEmpty()) {
                    changeLanguage(language, (String) event.getValue());
                }
            } else {
                //为空
                changeLanguage(language, (String) event.getValue());
            }

        }

    }


    /**
     * 如果是7.0以下，我们需要调用changeAppLanguage方法，
     * 如果是7.0及以上系统，直接把我们想要切换的语言类型保存在SharedPreferences中,然后重新启动MainActivity即可
     *
     * @param language
     */
    private void changeLanguage(String language, String languageType) {
        PreferenceUtils.applyString(AppConstants.LANGUAGE_KEY, language);
        PreferenceUtils.applyString(AppConstants.LANGUAGE_TYPE, languageType);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            LanguageUtil.changeAppLanguage(MyApplication.mContext, language);
        }
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(SettingActivity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                overridePendingTransition(0, 0);
                finish();
                dismissLoading();
            }
        }, AppConstants.AppStatus.DELAY_MILLIS);
    }

    private String setLanguageView(String value) {
        String language;
        if (value.equals("zh")) {
            language = LanguageType.CHINESE.getLanguage();
        } else if (value.equals("en")) {
            language = LanguageType.ENGLISH.getLanguage();
        } else {
//            Locale locale = getApplicationContext().getResources().getConfiguration().locale;
//            language = locale.getLanguage();
            language = LanguageType.CHINESE.getLanguage();
        }
        return language;
    }
}
